package com.healthcarex.hip.saas.security.model;

import com.healthcarex.hip.saas.common.model.Tree;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.persistence.Column;
import javax.persistence.Table;
import javax.persistence.Transient;
import java.io.Serializable;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author chenpan
 * @ClassName: SysResource
 * description:
 * @date 2018-04-02 14:39
 */
@Table(name = "HP_SYS_RESOURCE")
@Data
@ApiModel(value = "SysResource", description = "系统资源", parent = Tree.class)
public class SysResource extends Tree implements Serializable {

    @Column(name = "RESOURCE_NAME")
    @ApiModelProperty(value = "资源名称")
    private String resourceName;

    @Column(name = "RESOURCE_URL")
    @ApiModelProperty(value = "资源路径")
    private String resourceUrl;

    @Column(name = "REQUEST_TYPE")
    @ApiModelProperty(value = "请求方法 Get/Post/Put/Delete/Option")
    private String requestType;

    @Column(name = "RESOURCE_TYPE")
    @ApiModelProperty(value = "类型(1:应用类菜单 2.按钮)")
    private String resourceType;

    @Column(name = "PERMISSION")
    @ApiModelProperty(value = "权限标识符")
    private String permission;

    @Column(name = "ICON")
    @ApiModelProperty(value = "图标")
    private String icon;

    @Column(name = "ORDER_NO")
    @ApiModelProperty(value = "显示顺序")
    private Integer orderNo;

    @Column(name = "HIDDEN_MENU")
    @ApiModelProperty(value = "菜单是否隐藏")
    private Boolean hiddenMenu;

    @Column(name = "APP_ID")
    @ApiModelProperty(value = "所属应用")
    private Integer appId;

    @Transient
    @ApiModelProperty(value = "所属应用名称")
    private String appName;

    @Transient
    @ApiModelProperty(value = "多个应用ID集合(查询用)")
    private List<Integer> appIds;

    @Column(name = "VIEW_COMPONENT")
    @ApiModelProperty(value = "前端组件")
    private String viewComponent;

    @Column(name = "VIEW_URL")
    @ApiModelProperty(value = "前端地址")
    private String viewUrl;

    /**
     * 获取id参数字符串
     * @return
     */
    public String getAppIdsStr() {
        if (this.appId != null) {
            return this.appId.toString();
        }
        if (this.appIds != null) {

            return String.join(",", this.appIds.stream()
                    .map(x -> x.toString()).collect(Collectors.toList()));
        }
        return "";
    }
}
